﻿// Copyright (c) 2010
// by http://openlightgroup.net/

using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Net.Mail;
using System.Web.Mail;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Web.Security;

namespace SilverlightDebateForum
{
    public class Utility
    {
        #region ConvertToText
        public static string ConvertToText(string sHTML)
        {
            string sContent = sHTML;
            sContent = sContent.Replace("<br />", Environment.NewLine);
            sContent = sContent.Replace("<br>", Environment.NewLine);
            sContent = FormatText(sContent, true);
            return StripTags(sContent, true);
        }
        #endregion

        #region FormatText
        public static string FormatText(string HTML, bool RetainSpace)
        {
            //Match all variants of <br> tag (<br>, <BR>, <br/>, including embedded space
            string brMatch = "\\s*<\\s*[bB][rR]\\s*/\\s*>\\s*";
            //Replace Tags by replacement String and return mofified string
            return System.Text.RegularExpressions.Regex.Replace(HTML, brMatch, Environment.NewLine);
        }
        #endregion

        #region StripTags
        public static string StripTags(string HTML, bool RetainSpace)
        {
            //Set up Replacement String
            string RepString;
            if (RetainSpace)
            {
                RepString = " ";
            }
            else
            {
                RepString = "";
            }

            //Replace Tags by replacement String and return mofified string
            return System.Text.RegularExpressions.Regex.Replace(HTML, "<[^>]*>", RepString);
        }
        #endregion

        #region GetAllFileNames
        /// <summary>
        /// Find all files in a directory, and all files within every nested
        /// directory.
        /// from: http://dotnetperls.com/Content/Recursively-Find-Files.aspx
        /// </summary>
        /// <param name="baseDir">The starting directory you want to use.</param>
        /// <returns>A string array containing all the file names.</returns>
        public static string[] GetAllFileNames(string baseDir)
        {
            // Store results in the file results list.
            List<string> fileResults = new List<string>();

            // Store a stack of our directories.
            Stack<string> directoryStack = new Stack<string>();
            directoryStack.Push(baseDir);

            // While there are directories to process and we don't have too many results
            while (directoryStack.Count > 0 && fileResults.Count < 1000)
            {
                string currentDir = directoryStack.Pop();

                // Add all files at this directory.
                foreach (string fileName in Directory.GetFiles(currentDir, "*.*"))
                {
                    fileResults.Add(fileName);
                }

                // Add all directories at this directory.
                foreach (string directoryName in Directory.GetDirectories(currentDir))
                {
                    directoryStack.Push(directoryName);
                }
            }
            return fileResults.ToArray();
        }
        #endregion

        #region GetRandomPassword
        public string GetRandomPassword()
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            int intElements = random.Next(10, 26);

            for (int i = 0; i < intElements; i++)
            {
                int intRandomType = random.Next(0, 2);
                if (intRandomType == 1)
                {
                    char ch;
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
                else
                {
                    builder.Append(random.Next(0, 9));
                }
            }
            return builder.ToString();
        }
        #endregion

        #region HashPassword
        public static string HashPassword(int UserID, string Password)
        {
            // Note this password is 'salted' with the username when it is passed
            // to this method, so a basic dictionary attack would not work

            string HashedPassword = "";

            string UserPasswordAttempt = String.Format("{0}{1}", UserID.ToString(), Password);

            HashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(UserPasswordAttempt,
                System.Web.Configuration.FormsAuthPasswordFormat.MD5.ToString());

            return HashedPassword;
        }
        #endregion

        #region ConvertDNNUserToSilverlightDebateForumUser
        public static void ConvertDNNUserToSilverlightDebateForumUser(ref UserInfo objUserInfo, DotNetNuke.Entities.Users.UserInfo DNNUserInfo)
        {
            objUserInfo.Active = (DNNUserInfo.Membership.LockedOut == false);
            objUserInfo.DisplayName = DNNUserInfo.DisplayName;
            objUserInfo.Email = DNNUserInfo.Email;
            objUserInfo.FirstName = DNNUserInfo.FirstName;
            objUserInfo.IsSuperUser = DNNUserInfo.IsSuperUser;
            objUserInfo.LastName = DNNUserInfo.LastName;
            objUserInfo.PortalID = DNNUserInfo.PortalID;
            objUserInfo.Roles = DNNUserInfo.Roles;
            objUserInfo.UserID = DNNUserInfo.UserID;
            objUserInfo.Username = DNNUserInfo.Username;
        } 
        #endregion
    }
}